#include "useHead.h"
#include "stringtable.h"
#include "functable.h"
#include "instrStream.h"
#include "scriptHead.h"
#include "symTable.h"
#include "script.h"

void TScript::dump()
{
	_sh.dump();
	_st.dump();
	_hst.dump();
	_is.dump();
	_ft.dump();
	_syt.dump();
}



FILE_ERROR_TYPE TScript::readFile(const char *name)
{
	FILE *fp = fopen(name, "rb");
	if (fp == NULL) {
		fprintf(stderr, "Open File %s Error !\n", name);
		return E_R_OPEN_ERROR;
	}

	if (_sh.readFile(fp)) 
		return E_R_HEADER_ERROR;

	if (_st.readFile(fp)) 
		return E_R_STRINGTABLE_ERROR;
	
	if (_hst.readFile(fp)) 
		return E_R_HOSTAPI_ERROR;

	if (_is.readFile(fp, _st)) 
		return E_R_INSTRSTREAM_ERROR;
	
	if (_ft.readFile(fp)) 
		return E_R_FUNCTIONTABLE_ERROR;

	return E_SUCCESS;
}


FILE_ERROR_TYPE TScript::writeFile(const char *name)
{
	FILE *fp = fopen(name, "wb");

	if (fp == NULL) {
		fprintf(stderr, "Open File %s Error!\n", name);
		return E_W_OPEN_ERROR;
	}

	_sh.writeFile(fp);
	_st.writeFile(fp);
	_hst.writeFile(fp);
	_is.writeFile(fp);
	_ft.writeFile(fp);

	return E_SUCCESS;
}

